<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/js/layui-v2.4.5/layui/css/layui.css}" media="all" />
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all" />
    <link rel="stylesheet" th:href="@{/js/public/dtree/dtree.css}" media="all" />
    <link rel="stylesheet" th:href="@{/js/public/dtree/font/dtreefont.css}" media="all" />
</head>
<body  style="-webkit-backface-visibility: hidden;">

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>修改权限</legend>
</fieldset>

<form class="layui-form"  lay-filter="example">
    <div class="layui-form-item">
        <label class="layui-form-label">角色名</label>
        <div class="layui-input-block">
            <input type="text" name="privName" lay-verify="required" autocomplete="off" placeholder="请输入角色名"  class="layui-input" th:value="${priv.privName}">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">描述</label>
        <div class="layui-input-block">
            <input type="text" name="privDesc" lay-verify="required" autocomplete="off"  placeholder="请输入角色描述"  class="layui-input" th:value="${priv.privDesc}">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <input type="text" name="remark" autocomplete="off"  placeholder="备注"  class="layui-input" th:value="${priv.remark}">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选择权限</label>
        <div class="layui-input-block">
            <ul id="demoTree" class="dtree" data-id="0" style="border:1px solid #000"></ul>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block" style="text-align: center">
            <button type="button" class="layui-btn layui-btn-normal" id ="back">返回</button>
            <button   class="layui-btn" lay-submit lay-filter="save">保存</button>
        </div>
    </div>
</form>
<script th:src="@{/js/layui-v2.4.5/layui/layui.js}"></script>
<script th:src="@{/js/utils.js}"></script>
<script th:inline="javascript">
    var id = [[${id}]];
</script>
<script>
    layui.config({
        base: path+'/js/public/dtree/'
    }).extend({
        dtree: 'dtree'
    });
    layui.use(['form','laydate','dtree'], function(){
        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : parent.layer;
             dtree = layui.dtree,
            $ = layui.jquery, laydate = layui.laydate;


        $("#back").click(function () {
            var index=layer.getFrameIndex(window.name); //获取当前窗口的name
            layer.close(index);
        })


        /**
         * 初始化菜单树
         * */
        var DemoTree = dtree.render({
            url:path+"/priv/getAllMenu?id="+id,
            elem: "#demoTree",
            type: "all",
            initLevel: "5",
            checkbar: true,
            checkbarType: "all",
            response: {statusCode: 0, rootName: "dataTree", treeId:"treeId"} // 这里指定了返回的数据格式，组件会根据这些值来替换返回JSON中的指定格式，从而读取信息
        });


        /**
         *
         *监听提交
         * */
        form.on('submit(save)', function(data){
            var data = data.field;
            data.id=id;
            var params = dtree.getCheckbarNodesParam("demoTree");
            if(params.length<=0){
                layer.msg("请选择菜单权限",{icon: 5})
                return false;
            }
            $.each(params, function(i) {
                data['resourceIds['+i+']']=params[i].nodeId
            });
            console.log(data);
            var load = layer.load();

            $.ajax({
                type: "POST",
                url:  path+'/priv/savePriv',
                dataType:"json",
                data:data,
                success: function (data) {
                    layer.close(load);
                    if(data.code=="1"){
                        layer.msg(data.msg,{icon: 6})
                        window.parent.hasSave=true;
                        var index=layer.getFrameIndex(window.name); //获取当前窗口的name
                        layer.close(index);
                    }else{
                        layer.msg(data.msg,{icon: 5})
                    }
                },
                error: function (msg) {
                    layer.close(load);
                }
            });
            return false;
        });
    });

</script>

</body>
</html>